package com.music.mysongs.fragment.option

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.music.mysongs.R
import com.music.mysongs.data.getApiMusicPost
import com.music.mysongs.helper.myActReadIntSPF
import com.music.mysongs.helper.myActReadStringSPF
import com.music.mysongs.helper.myCxtReadIntSPF
import kotlinx.android.synthetic.main.fragment_post.*

class PostFragment : Fragment() {
    private val mTAG = "PostFragment"
    private val codePOST = 1

    private var uploadMessage: ValueCallback<Uri>? = null
    private var uploadMessageAboveL: ValueCallback<Array<Uri?>>? = null
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        webView_post.settings.apply {
            javaScriptEnabled = true
            useWideViewPort = true//将图片调整到适合的大小
            allowFileAccess = true//访问文件
            useWideViewPort = true//viewport标签
            domStorageEnabled = true
            allowFileAccessFromFileURLs = true
            //loadWithOverviewMode = true
        }
        //webView_post.isVerticalScrollBarEnabled = false//去除上下滑动
        //webView_post.isHorizontalScrollBarEnabled = false//去除左右滑动
        //webView_post.setInitialScale(100)//缩放倍率
        webView_post.webViewClient = WebViewClient()
        webView_post.webChromeClient = object : WebChromeClient() {
            // For Android >= 5.0
            override fun onShowFileChooser(
                webView: WebView,
                filePathCallback: ValueCallback<Array<Uri?>>,
                fileChooserParams: FileChooserParams
            ): Boolean {
                uploadMessageAboveL = filePathCallback
                openImageChooserActivity()
                return true
            }
        }
        val mid = myActReadIntSPF(this.activity, "spfUserId")
        if (mid != null) {
            webView_post.loadUrl(getApiMusicPost(mid))
        }
        btnBack_web.setOnClickListener {
            webView_post.goBack()
        }
        btnRefresh_web.setOnClickListener {
            webView_post.reload()
        }
    }

    private fun openImageChooserActivity() {
        val i = Intent(Intent.ACTION_GET_CONTENT)
        i.addCategory(Intent.CATEGORY_OPENABLE)
        i.type = "audio/*"
        startActivityForResult(Intent.createChooser(i, "选择上传的音频文件"), codePOST)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == this.codePOST) {
            if (uploadMessage == null && uploadMessageAboveL == null) {
                return
            }
            //取消选择时,返回null,否则<input file> 就是没有反应
            if (resultCode != AppCompatActivity.RESULT_OK) {
                if (uploadMessage != null) {
                    uploadMessage!!.onReceiveValue(null)
                    uploadMessage = null
                }
                if (uploadMessageAboveL != null) {
                    uploadMessageAboveL!!.onReceiveValue(null)
                    uploadMessageAboveL = null
                }
            }
            //选取时
            if (resultCode == AppCompatActivity.RESULT_OK) {
                var audioUri: Uri? = null
                when (requestCode) {
                    this.codePOST -> if (data != null) {
                        audioUri = data.data
                    }
                }
                //上传文件
                if (uploadMessage != null) {
                    uploadMessage!!.onReceiveValue(audioUri)
                    uploadMessage = null
                }
                if (uploadMessageAboveL != null) {
                    uploadMessageAboveL!!.onReceiveValue(arrayOf(audioUri))
                    uploadMessageAboveL = null
                }
            }
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        return inflater.inflate(R.layout.fragment_post, container, false)
    }
}